package bingosoft.metro.datasync.uuap.service;

import bingosoft.components.base.helper.ObjectHelper;
import bingosoft.components.base.service.Result;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.metro.datasync.base.service.IncSyncService;
import bingosoft.metro.datasync.uuap.UuapClientFactory;
import bingosoft.metro.datasync.uuap.client.form.PermissionDataForm;
import bingosoft.metro.datasync.uuap.client.form.PermissionRuleDataForm;
import bingosoft.metro.datasync.uuap.client.param.RequestParam;
import bingosoft.metro.datasync.uuap.client.service.DataClientService;
import bingosoft.metro.datasync.uuap.model.SyncPermissionModel;
import bingosoft.metro.datasync.uuap.model.SyncPermissionRuleModel;
import leap.core.AppContext;
import leap.orm.model.Model;

import java.util.*;

/**
 * @author Feily
 * @功能描述 权限增量同步数据服务类
 * @创建时间 2018-07-31
 */
public class PermissionSyncService extends IncSyncService {
	
	/**
	 * 权限规则列表
	 */
	protected List<SyncPermissionRuleModel> permissionRules = new ArrayList<SyncPermissionRuleModel>();
	
	/**
	 * 删除权限规则中间表SQL
	 */
	protected String deletePermissionRuleSyncDataSql;
	/**
	 * 删除权限规则目标表SQL
	 */
	protected String deletePermissionRuleTargetDataSql;
	/**
	 * 保存权限规则目标表SQL
	 */
	protected String savePermissionRuleTargetDataSql;
	
	/**
	 * 构建函数
	 */
	public PermissionSyncService() {
		this.setLogClass(this.getClass());
		
		this.tenantCode = AppContext.config().getProperty("uuap.tenantCode");
		
		this.getLastSyncTimeSql = this.getSqlContent("uuap-permission-getLastSyncTime");
		this.deleteSyncDataSql = this.getSqlContent("uuap-permission-deleteSyncData");
		this.saveTargetDataSql = this.getSqlContent("uuap-permission-saveTargetData");
		this.updateTargetDataSql = this.getSqlContent("uuap-permission-updateTargetData");
		this.deleteTargetDataSql = this.getSqlContent("uuap-permission-deleteTargetData");
		
		this.deletePermissionRuleSyncDataSql = this.getSqlContent("uuap-permissionRule-deleteSyncData");
		this.deletePermissionRuleTargetDataSql = this.getSqlContent("uuap-permissionRule-deleteTargetData");
		this.savePermissionRuleTargetDataSql = this.getSqlContent("uuap-permissionRule-saveTargetData");
	}
	
	@Override
	public Result deleteSyncData() {
		this.logMethodCalled("deleteSyncData");
	
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);
		
		// 删除权限规则同步表
		getDao().executeUpdate(this.deletePermissionRuleSyncDataSql, params);
		
		// 删除权限同步表
		getDao().executeUpdate(this.deleteSyncDataSql, params);
		
		return new Result();
	}

	@Override
	public Result saveSyncData(List<Model> syncData) {
		this.logMethodCalled("saveSyncData");
		
		// 保存权限
		getDao().batchInsert(syncData);
		
		// 保存权限规则
		getDao().batchInsert(this.permissionRules);
		
		return new Result();
	}

	@Override
	public Result syncTargetData() {
		this.logMethodCalled("syncTargetData");
	
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);
		
		// 修改目标权限数据
		getDao().executeUpdate(this.updateTargetDataSql, params);
		
		// 保存目标权限数据
		getDao().executeUpdate(this.saveTargetDataSql, params);

		// 删除目标权限规则数据
		getDao().executeUpdate(this.deletePermissionRuleTargetDataSql, params);
		
		// 删除目标权限数据
		getDao().executeUpdate(this.deleteTargetDataSql, params);
		
		// 保存目标权限规则关系数据
		getDao().executeUpdate(this.savePermissionRuleTargetDataSql, params);
		
		return new Result();
	}

	@Override
	public List<Model> getSyncDataList(Date lastSyncTime) {
		this.logMethodCalled("getSyncDataList");
		this.logParamValue("lastSyncTime", lastSyncTime);
		
		List<Model> list = new ArrayList<Model>();
		
		DataClientService service  = UuapClientFactory.getDataClientService();
		
		RequestParam requestParam = new RequestParam();
		requestParam.setUpdateBeginDate(lastSyncTime);
		requestParam.setUpdateEndDate(new Date());
		
		ResultEntity<List<PermissionDataForm>> resultEntity = service.queryPermissions(requestParam);
		if(!resultEntity.isSuccess()){
			throw new RuntimeException(resultEntity.getMessage());
		}
		
		// 构建数据
		List<PermissionDataForm> permissions = resultEntity.getBusinessObject();
		for (PermissionDataForm permissionDataForm : permissions) {
			
			// 构建权限
			SyncPermissionModel permission = new SyncPermissionModel();
			permissionDataForm.copyTo(permission);
			permission.setTenantCode(this.tenantCode);
			
			permission.setSyncId(ObjectHelper.getUUID());
			permission.setSyncTime(new Date());
			permission.setIsSync(true);
			list.add(permission);
			
			// 构建权限规则列表
			for (PermissionRuleDataForm permissionRuleDataForm : permissionDataForm.getRules()) {
				SyncPermissionRuleModel permissionRule = new SyncPermissionRuleModel();
				permissionRule.setPermissionRuleId(permissionRuleDataForm.getPermissionRuleId());
				permissionRule.setRuleName(permissionRuleDataForm.getRuleName());
				permissionRule.setRuleContent(permissionRuleDataForm.getRuleContent());
				permissionRule.setPriority(permissionRuleDataForm.getPriority());
				permissionRule.setStatus(permissionRuleDataForm.getStatus());
				permissionRule.setRemark(permissionRuleDataForm.getRemark());
				
				permissionRule.setPermissionId(permissionDataForm.getPermissionId());
				permissionRule.setCreateTime(permissionDataForm.getCreateTime());
				permissionRule.setCreateUserId(permissionDataForm.getCreateUserId());
				permissionRule.setUpdateTime(permissionDataForm.getUpdateTime());
				permissionRule.setUpdateUserId(permissionDataForm.getUpdateUserId());
				permissionRule.setTenantCode(this.tenantCode);
				
				permissionRule.setSyncId(ObjectHelper.getUUID());
				permissionRule.setSyncTime(new Date());
				permissionRule.setIsSync(permission.getIsSync());
				
				this.permissionRules.add(permissionRule);
			}
		}
		
		return list;
	}

	public List<SyncPermissionRuleModel> getPermissionRules() {
		return permissionRules;
	}

	public void setPermissionRules(List<SyncPermissionRuleModel> permissionRules) {
		this.permissionRules = permissionRules;
	}
}
